package com.sh.suhe.utildemos.views.activity.photo;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class StaggeredItemDecoration extends RecyclerView.ItemDecoration {
    private int space;
    public StaggeredItemDecoration(int space) {
        this.space=space;
    }
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        //设置上下间距
//        if (isFirstRaw(parent.getChildAdapterPosition(view))) {
            outRect.top = space;
//        } else {
            outRect.bottom = space;
//        }

        //判断左右列，方法1
        //设置列间距
        if (parent.getChildAdapterPosition(view) % 2 == 0) {
            //左列
            outRect.right = space / 2;
        } else {
            //右列
            outRect.left = space / 2;
        }

        //判断左右列，方法2（推荐）
//        GridLayoutManager.LayoutParams params = ((GridLayoutManager.LayoutParams) view.getLayoutParams());
//        if (params.getSpanIndex() != GridLayoutManager.LayoutParams.INVALID_SPAN_ID) {
//            //getSpanIndex方法不管控件高度如何，始终都是左右左右返回index
//            if (params.getSpanIndex() % 2 == 0) {
//                //左列
//                outRect.right = space / 2;
//            } else {
//                //右列
//                outRect.left = space / 2;
//            }
//        }
    }
}
